// -*- coding: utf-8 -*-
#include <event.h>
#include <event2/listener.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "conf.h"
#include "common.h"




void callback_socket_readable(evutil_socket_t fd, short what, void *_event)
{
        ssize_t recv_size;
        int buffer_size;
        char buffer[1024];
        int socket_data_size;
        uint8_t message_size;
        struct event *event = (struct event *)_event;
        int ret;
        

        buffer_size = sizeof(buffer_size);

        socket_data_size = get_socket_unread_data_size(fd, &socket_data_size);
        ret = peek_socket_data(fd, &message_size, 1);
        if (ret == 0) {
                goto error_peer_shutdown;
        }
        else {
                
        }
        recv_size = recv(fd, buffer, buffer_size, 0);
        if (recv_size == -1) {
                log("FAILURE: recv");
        }
        else if (recv_size == 0) {
                goto error_peer_shutdown;
        }
        else {
                send(fd, buffer, buffer_size, 0);
        }

        return;
error_peer_shutdown:
        info("peer shutdown!");
        close(fd);
        event_del(event);
        event_free(event);
        
}


void callback_new_connection(struct evconnlistener *listener,
                               evutil_socket_t fd,
                               struct sockaddr *sockaddr,
                               int socklen,
                               void * _base)
{
        struct event_base *base = (struct event_base *)_base;
        struct event *event;



        set_socket_nonblock(fd);
        set_socket_disable_linger(fd);
        set_tcp_socket_keepalive_count(fd);
        set_tcp_socket_keepalive_interval(fd, 4);
        set_tcp_socket_nodelay(fd);
        set_tcp_socket_quickack(fd);


        event = event_new(base, fd, EV_READ|EV_PERSIST, callback_socket_readable, NULL);
        if (NULL == event) {
                log("FAILURE: event_new");
                close(fd);
                return;
        }

        event_assign(event, base, fd, EV_READ|EV_PERSIST, callback_socket_readable, event);

        event_add(event, NULL);
}


void callback_accept_error(struct evconnlistener *listener, void *base)
{
        int error;
        int fd;

        
        fd = evconnlistener_get_fd(listener);
        error = evutil_socket_geterror(fd);
        if (error == ECONNABORTED) {
                fprintf(stderr, "accept error ECONNABORTED\n");
                return;
        }
        else if (error == ENOBUFS || error == ENOMEM) {
                fprintf(stderr, "accept error ENOBUFS\n");
        }
        else if (error == EMFILE || error == ENFILE) {
                fprintf(stderr, "accept error ENFILE, EMFILE\n");
        }
        else {
                fprintf(stderr, "accept error: %s\n", strerror(error));
                event_base_loopbreak((struct event_base *) base);
        }
}





int main (int argc, char **argv)
{
        struct event_base *event_base;
        struct evconnlistener *listener;
        struct sockaddr_in sin;

        event_enable_debug_mode();
        event_set_mem_functions(alloc_block,
                                realloc_block,
                                free_block);

        event_base = event_base_new();
        if (NULL == event_base) {
                log("event_base_new");
                exit(1);
        }
        
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr= htonl(0);
        sin.sin_port = htons(LISTEN_PORT);
        
        listener = evconnlistener_new_bind(event_base,
                                           callback_new_connection,
                                           event_base,
                                           LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
                                           LISTEN_BACKLOG,
                                           (struct sockaddr *) &sin,
                                           sizeof(struct sockaddr_in));
        if (NULL == listener) {
                log("evconnlistener_new_bind");
                exit(1);
        }


        evconnlistener_set_error_cb(listener, callback_accept_error);
        event_base_dispatch(event_base);
        return 0;
}
